module solution_m

    implicit none

contains

    !> 来源：https://zhuanlan.zhihu.com/p/498776301
    !> Copyright (c) 2022, 123. All rights reserved.
    pure subroutine Solution(A, B, X, n)
        integer, intent(in) :: n
        real(8), intent(inout) :: A(n, n), B(n), X(n)
        real(8) :: M(n, n), p
        integer i, j, k
        do k = 1, n - 1
            do i = 1, k + 1
                M(i, k) = A(i, k)/A(k, k)
            end do
        end do
        do k = 1, n - 1
            do i = k + 1, n
                do j = k + 1, n
                    A(i, j) = A(i, j) - A(k, j)*M(i, k)
                end do
                B(i) = B(i) - B(k)*M(i, k)
            end do
        end do
        X(n) = B(n)/A(n, n)
        do k = n - 1, 1, -1
            p = 0
            do i = k + 1, n
                p = p + X(k)*A(k, i)
            end do
            X(k) = (B(k) - p)/A(k, k)
        end do
    end subroutine

end module solution_m
